define(['jquery', 'bootstrap', 'backend','amazeui','template','form','layer','table'], function ($, undefined, Backend ,Amazeui,Template,Form,Layer,Table) {
     var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
  
        buy:function(){
            $("#buyaddvert").data("validator-options", validatoroptions);

              Form.api.bindevent($("#buyaddvert"), function (data, ret) {
                setTimeout(function () {
                    location.href = ret.url ? ret.url : "/";
                }, 1000);
              });
        },
        publish:function(){
          $('#imgestext').on('click',function(){
          
              var data={}
              data['type']='imagetext';
              data['title']=$("#user-name").val();
              data['content']=$("#ling").val();
              data['images']=$("#images").val();
              
              data['video']='456';
              data['totalnum']=$("#totalnum").val();
              data['status']='pass';
             $.post('advertise/publish',data,function(rsp){
                 layer.closeAll();
                    layer.msg(rsp.msg, {
                      offset: 'auto',
                      anim: 6,
                      end:function(){
                       
                      }
                 });

                 if (rsp.code==0) {}else{
                       window.location.reload();
                    }     
            })

          })
          $('#imgeslink').on('click',function(){
              var data={}
              data['type']='link';
              data['title']=$("#name").val();
              data['content']=$("#link").val();
              data['images']='link';
              
              data['video']='456';
              data['totalnum']=$("#totalnum_link").val();
              data['status']='pass';
             $.post('advertise/publish',data,function(rsp){
                 layer.closeAll();
                    layer.msg(rsp.msg, {
                      offset: 'auto',
                      anim: 6,
                      end:function(){
                       
                      }
                 });
                  if (rsp.code==0) {}else{
                       window.location.reload();
                    }   
            })

          })
        },
        showadvertise:function(){
            // $.post('advertise/showadvertise',{},function(rsp){
            //     var data = {
            //         info: rsp.info,

            //     };
            //     $('#adinfo').html(Template('adtpl', data));
            // })

            var running = false;
            $('#nextad').on('click',function(){
                if(running){
                    return false;
                }
                running =true;
                var load =Layer.load()
                $('#progress').css('width','0');
                $.post('advertise/showadvertise',{},function(rsp){
                    var data = {
                        info: rsp.info,

                    };
                    $('#adinfo').html(Template('adtpl', data));
                    var i =1;                    
                    var sid = setInterval(function(){
                        i++
                        $('#progress').css('width',i +'%');
                        if (i >= 100) {
                            clearInterval(sid);
                            running = false;
                            $.post('advertise/finishad',{id:rsp.info.id},function(adrsp){
                                $('#shengyu').text(adrsp.surplus);
                                Layer.msg(adrsp.msg);
                                Layer.msg('您已经看完本条广告，继续观看点下一条。');
                                
                            })

                        }
                    },rsp.time*10);
                    Layer.close(load)
                })
            })
            $('#nextad').click()
        },

    };
    return Controller;
});